Alright so following up to my previous question I've made some progress, game has the addition of left and right movement and a simple jump script, evolution woo :D
My latest addition was an Idle animation and now the dude can't move left :D
I hope its not too silly I checked everything but can't point out the issue >.> ..
regardless here's the code, Thanks in advance, truly appreciative! (origination is awful srry >.<):
import pygame
pygame.init()
path = "C:/Users/user/Documents/Le game/"
win = pygame.display.set_mode((800, 700))
pygame.display.set_caption("Potato ultra")
bg = pygame.image.load(path + "BG.jpg")
walk_right = [pygame.image.load("C:/Users/user/Documents/Le game/R2.png"), pygame.image.load("C:/Users/user/Documents/Le game/R3.png"), pygame.image.load("C:/Users/user/Documents/Le game/R4.png"), pygame.image.load("C:/Users/user/Documents/Le game/R5.png"), pygame.image.load("C:/Users/user/Documents/Le game/R6.png"), pygame.image.load("C:/Users/user/Documents/Le game/R7.png"), pygame.image.load("C:/Users/user/Documents/Le game/R8.png"), pygame.image.load("C:/Users/user/Documents/Le game/R9.png")]
walk_left = [pygame.image.load("C:/Users/user/Documents/Le game/L2.png"), pygame.image.load("C:/Users/user/Documents/Le game/L3.png"), pygame.image.load("C:/Users/user/Documents/Le game/L4.png"), pygame.image.load("C:/Users/user/Documents/Le game/L5.png"), pygame.image.load("C:/Users/user/Documents/Le game/L6.png"), pygame.image.load("C:/Users/user/Documents/Le game/L7.png"), pygame.image.load("C:/Users/user/Documents/Le game/L8.png"), pygame.image.load("C:/Users/user/Documents/Le game/L9.png")]
Static = pygame.image.load("C:/Users/user/Documents/Le game/Idle.png")
SW = 800
SH = 700
x = 0
y = 480
width = 64
height = 64
vel = 20
isJump = False
MoveLeft = False
MoveRight = False
Idle = False
JumpCount = 10
walkCount = 0
def redrawGameWindow():
win.blit(bg, (0,0))
global walkCount
if not Idle:
if MoveRight:
if walkCount <= 7:
win.blit(walk_right[walkCount], (x, y))
elif walkCount > 7:
walkCount = 0
win.blit(walk_right[walkCount], (x, y))
if MoveLeft:
if walkCount <= 7:
win.blit(walk_left[walkCount], (x, y))
elif walkCount > 7:
walkCount = 0
win.blit(walk_left[walkCount], (x, y))
else:
win.blit(Static, (x, y))
pygame.display.update()
run = True
while run:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > 0:
Idle = False
MoveRight = False
MoveLeft = True
x -= vel
walkCount += 1
if keys[pygame.K_RIGHT] and x < SW - width:
Idle = False
MoveRight = True
MoveLeft = False
x += vel
walkCount += 1
else:
Idle = True
if not isJump:
if keys[pygame.K_UP] and y > 0:
y -= vel
if y < 0:
y = 0
if keys[pygame.K_DOWN] and y < SH - height:
y += vel
if y > 636:
y = 636
if keys[pygame.K_SPACE]:
isJump = True
else:
if JumpCount >= -10:
y -= (JumpCount * 4)
if y < 0:
y = 0
JumpCount -= 2
else:
isJump = False
JumpCount = 10
redrawGameWindow()
pygame.quit()
Well, the first thing I would do would be to add the following code (before the final line below, which you already have):
print("DEBUG1 keyl =", keys[pygame.K_LEFT], "x =", x)
print("DEBUG2 keyr =", keys[pygame.K_RIGHT], "SW =", Sw, "wid =", width)
print("DEBUG3 mover =", MoveRight, "movel =", MoveLeft)
print("DEBUG4 vel =", vel, "walkc =", walkCount)
print("DEBUG5 ==========")
if keys[pygame.K_LEFT] and x > 0:
That way, you'll see all the variables that take part in deciding left and right moves, and it will hopefully become obvious what is preventing the left move from functioning.
Based on a slightly deeper look at your code, it appears to be this bit:
if keys[pygame.K_LEFT] and x > 0:
Idle = False
MoveRight = False
MoveLeft = True
x -= vel
walkCount += 1
if keys[pygame.K_RIGHT] and x < SW - width:
Idle = False
MoveRight = True
MoveLeft = False
x += vel
walkCount += 1
else:
Idle = True
Since that else belongs only to the second if statement, it will fire whenever the right key is not being pressed, regardless of whether you're pressing the left key.
I suspect you can fix this simply by changing it from an if, if, else sequence to an if, elif, else sequence, so that the else fires only if neither of the keys are pressed.
A couple of possible improvements to your code:
you can get rid of all that walkCount checking and adjusting in the redraw function by simply using walkCount = (walkCount + 1) % 8 in the event loop - this will ensure it wraps from seven to zero without further effort.
you don't appear to have limiters on the x value. For example, if x == 5 and vel == 10, it's possible that a left move will set x = -5 which may not be desirable. You have more knowledge of the game play than me so I could be wrong, but it's worth checking.
you may not need both MoveLeft and MoveRight. The Idle flag decides whether you're moving or not so, if you are moving, it's either left or right. So a Idle/MoveRight combo should be enough for the code to figure out what to do. Again, this is a gameplay issue, something to look at but I may be incorrect.
not sure how your sprites look when they jump but you may be better off using constant acceleration formulae for calculating y position. I've answered similar questions before so you can have a look at this answer for guidance.
Related
Hello I am new to stack overflow and python and I need some help with this coding error I get. I followed Tech with Tim's tutorial on creating sprite movement and I'm coming across an error message when I move my sprite around.
Here's a link to the video I followed:
https://www.youtube.com/watch?v=UdsNBIzsmlI
The jump movement seems to be working but I get an error message when I move my character/sprite more than 3 steps to the left/right.
Here's the error message I get:
Traceback (most recent call last):
File "C:\Users\Desktop\PYGAME Folder\Game Code.py", line 90, in <module>
redrawGameWindow()
File "C:\Users\Desktop\PYGAME Folder\Game Code.py", line 40, in redrawGameWindow
win.blit(walkRight[walkCount//3], (x,y))
TypeError: argument 1 must be pygame.Surface, not str
Here's the code using Tech with Tim's example:
import pygame
pygame.init()
win = pygame.display.set_mode((500,480))
pygame.display.set_caption("First Game")
walkRight = [pygame.image.load('R1.png'), ('R2.png'), ('R3.png'), ('R4.png'), ('R5.png'), ('R6.png'), ('R7.png'), ('R8.png'), ('R9.png')]
walkLeft = [pygame.image.load('L1.png'), ('L2.png'), ('L3.png'), ('L4.png'), ('L5.png'), ('L6.png'), ('L7.png'), ('L8.png'), ('L9.png')]
bg = pygame.image.load('bg.jpg')
char = pygame.image.load('standing.png')
x = 50
y = 400
width = 40
height = 60
vel = 5
clock = pygame.time.Clock()
isJump = False
jumpCount = 10
left = False
right = False
walkCount = 0
def redrawGameWindow():
global walkCount
win.blit(bg, (0,0))
if walkCount + 1 >= 27:
walkCount = 0
if left:
win.blit(walkLeft[walkCount//3], (x,y))
walkCount += 1
elif right:
win.blit(walkRight[walkCount//3], (x,y))
walkCount += 1
else:
win.blit(char, (x, y))
walkCount = 0
pygame.display.update()
run = True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False
elif keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
left = False
right = True
else:
left = False
right = False
walkCount = 0
if not(isJump):
if keys[pygame.K_SPACE]:
isJump = True
left = False
right = False
walkCount = 0
else:
if jumpCount >= -10:
y -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
else:
jumpCount = 10
isJump = False
redrawGameWindow()
pygame.quit()
In walkRight, only the first image is actually loaded. The others are just the filenames. There is the same issue with walkLeft.
See if this resolves the problem:
walkRight = [pygame.image.load(i) for i in ['R1.png', 'R2.png', 'R3.png', 'R4.png', 'R5.png', 'R6.png', 'R7.png', 'R8.png', 'R9.png']]
For more concise code try this:
walkRight = [pygame.image.load("R%d.png"%i) for i in range(1,10)]
This question already has answers here:
Pygame doesn't let me use float for rect.move, but I need it
(2 answers)
Closed 2 years ago.
Warning (from warnings module):
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python38-32\rect 2.py", line 57
pygame.draw.rect(win,(0, 0, 255), (x, y, width, height))
DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using
__int__ is deprecated, and may be removed in a future version of Python.
This is the error I am getting from Shell and below is the code I typed:
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("nyumph")
x = 50
y = 425
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y += vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -=1
else:
isJump = False
jumpCount = 10
win.fill((0, 0, 0))
pygame.draw.rect(win,(0, 0, 255), (x, y, width, height))
pygame.display.update()
pygame.quit()
This code is supposed to display a rectangle and make it jump on pressing the keys. I am not able to understand the error and what is the meaning of it and I also don't know what I'm supposed to do? The jumping is not working somehow. Please help me with it.
This is not an error, it is a warning. The warning is caused by the fact that the rectangle argument for pygame.draw.rect has to be a tuple with integral values. In your application the coordinates (x, y) are floating point values.
You can get rid of the warning by rounding the x and y coordinate to integral values. Use the function round:
pygame.draw.rect(win,(0, 0, 255), (x, y, width, height))
pygame.draw.rect(win,(0, 0, 255), (round(x), round(y), width, height))
The jumping does not work in your application because of the wrong Indentation in the if statements.
See the working example:
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("nyumph")
x = 50
y = 425
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
#<--| INDENTATION
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y += vel
if keys[pygame.K_SPACE]:
isJump = True
#<------| INDENTATION
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -=1
else:
isJump = False
jumpCount = 10
win.fill((0, 0, 0))
pygame.draw.rect(win,(0, 0, 255), (round(x), round(y), width, height))
pygame.display.update()
pygame.quit()
I am making a 2D platform game using the Python module Pygame. I have made the background, the character and the movement for said player. You can move using a & d on the keyboard and jump with SPACE. However I cannot figure out how to make an idle animation for the player. I have the running animation, but I haven't made the sprites yet, so I just used the idle picture set.
Here's my code:
import pygame, time, itertools
pygame.init()
# background image
walkRight = pygame.image.load('idle1.png')
walkLeft = pygame.image.load('idle1.png')
bg = pygame.image.load("background.png")
idle = [pygame.image.load('idle1.png'), pygame.image.load('idle2.png'), pygame.image.load('idle3.png')]
standcount = True
clock = pygame.time.Clock()
# jump
isJump = False
jumpcount = 10
# window
display_width = 1000
display_height = 600
win = pygame.display.set_mode((display_width, display_height))
# title & icon
pygame.display.set_caption("Grand Theft Ewok")
icon = pygame.image.load('bear.png')
pygame.display.set_icon(icon)
# player creds
x = 50
y = 430
vel = 10
# playerIMG
playerIMG = pygame.image.load('player.png')
def player(x, y):
global standcount
global walkcount
win.blit(bg, (0, 0))
# win.blit(playerIMG, (x,y))
if walkcount + 1 >= 9:
walkcount = 0
if standcount + 1 >= 9:
standcount = 0
if left:
win.blit(idle[walkcount // 3], (x, y))
walkcount += 1
elif right:
win.blit(idle[walkcount // 3], (x, y))
walkcount += 1
elif standcount:
p = 0
for frame in idle:
win.blit(idle[p], (x, y))
p += 1
if p >= 2:
p = 0
continue
pygame.display.update()
# game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# movement
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and x > (vel - 25):
x -= vel
left = True
right = False
elif keys[pygame.K_d] and x < 835:
x += vel
right = True
left = False
else:
right = False
left = False
walkcount = 0
standcount = True
if not (isJump):
if keys[pygame.K_SPACE]:
isJump = True
left = False
right = False
else:
if jumpcount >= -10:
neg = 1
if jumpcount < 0:
neg = -1
y -= (jumpcount ** 2) * 0.5 * neg
jumpcount -= 1
else:
isJump = False
jumpcount = 10
player(x, y)
pygame.display.update()
pygame.quit()
under the player function you can see where I tried to use a for loop, but it just doesn't work.
I am using Python 3.8.
Best wishes to all.
Stay indoors, Thanks.
EDIT
I have found out how to make the idle animation using this:
elif standcount:
p = 0
for frame in idle:
win.blit(idle[standcount], (x, y))
standcount += 1
#pygame.display.update()
if standcount >= 2:
standcount = 0
continue
pygame.display.update()
However, it iterates through the list extremely fast. I cant think of a way to slow it down without using time.sleep because then it will freeze the game every time I stop moving.
Thanks
You dont need a loop, just do the same thing you did for the walking:
if left:
win.blit(idle[walkcount // 3], (x, y))
walkcount += 1
elif right:
win.blit(idle[walkcount // 3], (x, y))
walkcount += 1
else:
win.blit(idle[standcount // 3], (x, y))
standcount += 1
using a loop means it will blit all the images on top of each other in the same frame meaning you will only see the top/ last one. What you did for the walking animations works perfectly.
also, you should only have one pygame.display.update(). You should only update the screen at the end of each frame, not many times in a frame. Since you call player() right before updating the screen, you can get rid of one of them, as one will do nothing
So i found the problem, you had a standcount = True which would reset it to 1, getting rid of this solved the problem
Here is the full code edited:
import pygame, time, itertools
pygame.init()
# background image
walkRight = pygame.image.load('idle1.png')
walkLeft = pygame.image.load('idle1.png')
bg = pygame.image.load("background.png")
idle = [pygame.image.load('idle1.png'), pygame.image.load('idle2.png'), pygame.image.load('idle3.png')]
standcount = 0 # change it to an int, not a bool
clock = pygame.time.Clock()
# jump
isJump = False
jumpcount = 10
# window
display_width = 1000
display_height = 600
win = pygame.display.set_mode((display_width, display_height))
# title & icon
pygame.display.set_caption("Grand Theft Ewok")
icon = pygame.image.load('bear.png')
pygame.display.set_icon(icon)
# player creds
x = 50
y = 430
vel = 10
# playerIMG
playerIMG = pygame.image.load('player.png')
def player(x, y):
global standcount
global walkcount
win.blit(bg, (0, 0))
# win.blit(playerIMG, (x,y))
if walkcount + 1 >= 9:
walkcount = 0
if standcount + 1 >= 9:
standcount = 0
if left:
win.blit(idle[walkcount // 3], (x, y))
walkcount += 1
elif right:
win.blit(idle[walkcount // 3], (x, y))
walkcount += 1
else:
win.blit(idle[standcount // 3], (x, y))
standcount += 1
pygame.display.update()
# game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# movement
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and x > (vel - 25):
x -= vel
left = True
right = False
elif keys[pygame.K_d] and x < 835:
x += vel
right = True
left = False
#you could put standcount = 0 here to reset animation after walking
else:
right = False
left = False
walkcount = 0
#got rid of standcount = True
if not (isJump):
if keys[pygame.K_SPACE]:
isJump = True
left = False
right = False
else:
if jumpcount >= -10:
neg = 1
if jumpcount < 0:
neg = -1
y -= (jumpcount ** 2) * 0.5 * neg
jumpcount -= 1
else:
isJump = False
jumpcount = 10
player(x, y)
pygame.display.update()
pygame.quit()
As for speed:
There are 2 options,
A) wait a certain amount time (i recommend)
B) wait a certain amount of frames (don't recommend)
Its best to do time as then performance doesn't affect the speed
So you already have time imported you can do this:
idle_frame_start = time.time() # get the current time - very accurate
walk_frame_start = time.time()
def player(x, y):
global standcount, idle_frame_start
global walkcount, walk_frame_start
# win.blit(playerIMG, (x,y))
if walkcount + 1 >= 4: #if the count is more than amount of images
walkcount = 0
if standcount + 1 >= 4:
standcount = 0
if left:
win.blit(idle[walkcount], (x, y))
if time.time() - walk_frame_start > 0.8:
walkcount += 1
walk_frame_start = time.time()
elif right:
win.blit(idle[walkcount], (x, y))
if time.time() - walk_frame_start > 0.8:
walkcount += 1
walk_frame_start = time.time()
else:
win.blit(idle[standcount], (x, y))
if time.time() - idle_frame_start > 0.8: # if the time difference is bigger than 0.8s
standcount += 1
idle_frame_start = time.time() # reset the start time
pygame.display.update()
Im trying out pygame for the first time, now ive gotten very comfortable with python as a language, and ive been trying to recreate Pong, like the old school game with 2 paddles and a ball. I cant seem to figure out how to get the ball to reflect off the paddle. Im not looking for angles and stuff yet, cos ill be able to figure that out on my own.
Buttt what ive thought of is to get a range of coordinates, which are the X & Y of the paddle and the x & y + the width and height, and if the ball enters these, it simply reflects as it does at a boundary. Ive tried doing multiple if statements, as you can see in the code below, but ive also tried doing it as a single statement, but that doesnt work. None of the debug prints ive put in actually print, but when i test the coord ranges with print they look fine :D
Ill paste my code here so you guys can run my game as is.
Id really appreciate your guys help!
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
x = 50
y = 50
width = 10 #sets variables for the main paddle
height = 60
vel = 5
ballx = 250
bally = 250
radius = 5
direction = True #True is left, False is right
bvel = 4 #sets variables for the ball
angle = 0
coordxGT = 0
coordxLT = 0 #sets variables for the coordinate ranges of the first paddle for collision
coordyGT = 0
coordyLT = 0
def setCoords():
coordxGT = x
coordxLT = x + width
coordyGT = y #This function updates the coords throughout the main loop
coordyLT = y + height
coordxLT += radius
coordyLT += radius
run = True
while run == True:
pygame.time.delay(20)
for event in pygame.event.get(): #on quit, quit the game and dont throw up an error :)
if event.type == pygame.QUIT:
run = False
setCoords()
if direction == True: #Ball movement
ballx -= bvel
else:
ballx += bvel
if ballx<0:
ballx += bvel
direction = False
elif bally<0:
bally += bvel
elif ballx>495: #if the ball hits an edge
ballx -= bvel
direction = True
elif bally>495:
bally -= bvel
if ballx<coordxLT and ballx>coordxGT:
print("S1")
if bally<coordyLT and bally>coordyGT: #THE PART I CANT FIGURE OUT. If the ball lands within these ranges of coordinates, reflect it and change its direction
print("S2")
if direction == True:
print("YES")
ballx += bvel
direction = False
keys = pygame.key.get_pressed() #gets the keys pressed at that current time
if keys[pygame.K_DOWN]:
bally += bvel #Ball control (For debugging)
if keys[pygame.K_UP]:
bally -= bvel
if keys[pygame.K_w]:
y -= vel
if keys[pygame.K_a]:
x -= vel
if keys[pygame.K_s]: #Paddle controls
y += vel
if keys[pygame.K_d]:
x += vel
if x<0:
x += vel
if y<0:
y += vel
if x>80:
x -= vel #Stops the paddle from moving if it hits a boundary
if y>440:
#440 because window height - height of cube
y -= vel
win.fill((0,0,0))
pygame.draw.circle(win, (255, 255, 255), (ballx, bally), radius) #refreshes the screen
pygame.draw.rect(win,(255,255,255),(x, y, width, height))
pygame.display.update()
pygame.quit()
You are close, but you missed to declare the variables coordxGT, coordxLT, coordxLT, coordyLT to be global.
def setCoords():
global coordxGT, coordxLT, coordxLT, coordyLT
coordxGT = x
coordxLT = x + width
coordyGT = y
coordyLT = y + height
coordxLT += radius
coordyLT += radius
Note, if you want to write to a variable in global namespace in a function, then the varible has be interpreted as global. Otherwise an new variable in the scope of the function will be created and set. See global statement.
I have been have a lot of trouble with my summative project where you have to make a game in python using pygame. I am currently stuck with trying to make a menu screen for my game, the problem is I can't get my buttons to show up and after that how to change to my game map once they click start. I have been getting the type error "TypeError: button() takes from 6 to 7 positional arguments but 9 were given" if any of you could help that would mean a lot. thanks.
#army man man
import pygame
from pygame import *
from tkinter import *
import time
import random
win = pygame.display.set_mode((1280,720))
pygame.display.set_caption("Armymanman")
walkright = [pygame.image.load('walk1.png'),pygame.image.load('walk2.png'),pygame.image.load('walk3.png'),pygame.image.load('walk4.png'),pygame.image.load('walk5.png'),pygame.image.load('walk6.png'),pygame.image.load('walk7.png'),pygame.image.load('walk8.png'),pygame.image.load('walk9.png')]
walkleft = [pygame.image.load('walk1L.png'),pygame.image.load('walk2L.png'),pygame.image.load('walk3L.png'),pygame.image.load('walk4L.png'),pygame.image.load('walk5L.png'),pygame.image.load('walk6L.png'),pygame.image.load('walk7L.png'),pygame.image.load('walk8L.png'),pygame.image.load('walk9L.png')]
backg = pygame.image.load('pythonbg.png')
#resize char image
char = pygame.image.load('stand.png')
char = pygame.transform.scale(char,(64,64))
x=0
y=720
w=1280
h=720
black=(0,0,0)
red=(255,0,0)
blue=(0,0,255)
green=(0,255,0)
white=(255,255,255)
clock = pygame.time.Clock()
#plays music does not work?
#def playmusic():
pygame.init()
pygame.mixer.pre_init(22050, -16, 2, 2048) # setup mixer
pygame.mixer.init()
pygame.mixer.music.load('bgmusic.ogg')
pygame.mixer.music.play(-1)
#playmusic()
##song=pygame.mixer.sound('bgmusic.ogg')
##
##song.play()
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.move = 5
self.isjump = False
## self.isjump=0
self.jumpcount = 10
self.left = False
self.right = False
self.walkcount = 0
def draw(self,win):
if self.walkcount + 1 >=27:
self.walkcount = 0
if self.left:
win.blit(walkleft[self.walkcount//3], (self.x,self.y))
self.walkcount += 1
elif self.right:
win.blit(walkright[self.walkcount//3], (self.x,self.y))
self.walkcount += 1
else:
win.blit(char, (self.x,self.y))
def redrawwindow():
win.blit(backg, (0,0))
man.draw(win)
pygame.display.update()
startsc=pygame.image.load('armymanbg.jpg')
startsc=pygame.transform.scale(startsc,[1280,720])
win.blit(startsc,(0,0))
pygame.display.flip()
DIS=1
def button(x,y,w,h,ic,ac,action=None):
mouse=pygame.mouse.get_pos()
click= pygame.mouse.get_pressed()
if x+w>mouse[0] > x and y+h > mouse[1] > y:
global DIS
global intro
pygame.draw.rect(win,ac,(x,y,w,h))
if click[0] == 1 and action!= None:
if action=="START":
pygame.display.update()
time.sleep(0.5)
run= False
DIS=2
elif action=="EXIT":
pygame.quit()
quit()
elif action=="Menu":
DIS=1
else:
return True
else:
pygame.draw.rect(window, ic, (x,y,w,h))
text = pygame.font.SysFont('freesonsbold.ttf',25)
win.blit(text.render("BECOME THE ARMYMANMAN!", True,(black)), (x+(w/3.1), y+(h/2.5)))
win.blit(text.render("Start",True,(black)), (x+(w/3.1), y+(h/2.5)))
win.blit(text.render("Exit",True,(black)), (x+(w/3.1), y+(h/2.5)))
#MAIN LOOP
man = player(0, 656, 64, 64)
run = True
while run:
clock.tick(27)
if DIS==1:
for event in pygame.event.get():
if event.type==pygame.QUIT:
quit()
win.fill((0,0,0))
button("START",400, 500, 150, 75,green,blue,"START")
button("EXIT", 400,650,150,75,green,red,"EXIT" )
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if (event.type==pygame.KEYDOWN and event.key == K_RIGHT)and man.x < 1280 - man.width - man.move:
man.x += man.move
man.right = True
man.left = False
elif (event.type==pygame.KEYDOWN and event.key == K_LEFT)and man.x > man.move:
man.x -= man.move
man.left = True
man.right = False
else:
man.right= False
man.left = False
man.walkcount = 0
if (event.type==pygame.KEYDOWN and event.key == K_SPACE):
man.isjump = True
man.right = False
man.left = False
man.walkcount = 0
else:
if(man.isjump==True):
if man.jumpcount >= -10:
neg = 1
if man.jumpcount < 0:
neg = -1
man.y -= (man.jumpcount ** 2) *0.5 * neg
man.jumpcount -= 1
else:
man.isjump = False
man.jumpcount = 10
redrawwindow()
print(man.y)
pygame.quit
For starters, you really shouldn't do from pygame import *, as it will pollute your namespace with lots of stuff you don't want, from pygame.locals import * is more normal. The TypeError you mention is due to you giving button() too many parameters. You can see that it takes six or seven parameters when you define it (def button(x,y,w,h,ic,ac,action=None):). When you call it you try to pass too many (button("START",400, 500, 150, 75,green,blue,"START")). I think the initial "START" parameter is the excess. Here are some other things I've noticed:
if(man.isjump==True): can be replaced with if man.isjump:. There seem to be lots of similar excess brackets. You do not need them around comparisons unless it's something like if x and (a or b), where it makes a difference. You also do not need them around most function parameters. This button-drawing lines with (black) are an example of this.
The final pygame.quit is unnecessary and will not work anyway (if you needed it, which you don't, it would be pygame.quit().
All of your buttons are drawn in the same place; try changing the draw coordinates to be different
Your whitespace is strange. As a general rule, do not have trailing whitespace at the end of lines or a gap of more than two blank lines.
In this line (pygame.draw.rect(window, ic, (x,y,w,h))), you draw to window instead of win, which breaks it.
The whole section of code after the huge whitespace glob (excluding pygame.quit, which should be removed) needs to be indented.
if (event.type==pygame.KEYDOWN and event.key == K_RIGHT) can be replaced with if pygame.key.get_pressed()[K_RIGHT] and similar for left. These, along with redrawwindow() should be outside of the for event in pygame.event.get(): loop.