pygame

Zooming in and out of a PyGame window with all objects still in place

无人久伴 提交于 2021-02-05 07:58:47
问题 I am having an issue with PyGame i can't resolve. So: my idea is that I have a map I can zoom in/out on. zooming in works fine. But zooming out shows that the rest of the picture got deleted and only the part of the image that was previously visible on the window exists now. This is my code: import pygame from pygame.locals import * import os class App: def __init__(self): self.running = True self.size = (800,600) #create window self.window = pygame.display.set_mode(self.size, pygame

Trying to play a sound wave on python using pygame

人盡茶涼 提交于 2021-02-05 07:56:27
问题 I'm following this example from the book 'math for programmers' but it doesn't work for me: import pygame, pygame.sndarray pygame.mixer.init(frequency=44100, size=-16, channels=1) import numpy as np arr = np.random.randint(-32768, 32767, size=44100) sound = pygame.sndarray.make_sound(arr) sound.play() It returns these errors: ... in make_sound return numpysnd.make_sound(array)" ... in make_sound return mixer.Sound(array=array) ValueError: Array must be 2-dimensionarl for stereo mixer" The

how to clear text on a window in pygame

守給你的承諾、 提交于 2021-02-05 07:54:12
问题 In my game That I am making. I am trying to resolve this problem that I Have with the game over screen. the problem is when you die it says 'You died' then I put 'Play again' or 'Quit'. If you press Quit Obviously it quits the game but if you press Play again what I want it to do is to rerun the program. def text_objects(text, font): textSurface = font.render(text, True, black) return textSurface, textSurface.get_rect() def button(msg, x, y, w, h, ic, ac, action=None): mouse = pygame.mouse

TypeError: must be pygame.Surface, not tuple. Python/Pygame noobie

允我心安 提交于 2021-02-05 07:49:26
问题 Hello stack overflow users! I have written this code and it programs to draw lines whenever you click on the pygame screen, but when I run the program, I get an error message saying "TypeError: must be pygame.Surface, not tuple." . I attempted to mess around with the program, but i had no luck. I would greatly appreciate it if anyone can help me figure out what's wrong with the code. Thanks! The code is on the bottom. Thanks again! import pygame pygame.init() pygame.display.set_mode((800,600)

Random movement pygame

天大地大妈咪最大 提交于 2021-02-05 07:40:20
问题 I'm trying to make a simple life simulator, and I need the "cells" to move almost randomly (with a few rules) across the screen. But the problem is, after a while they tend to bunch up in the upper-left corner of the screen. I tried to change a lot of things, like completely skipping the rules and making them move completely randomly, but they still bunch up. Is there some obvious problem in my code? Or should I be doing the movement completely differently? The code with irrelevant parts cut

Random movement pygame

天涯浪子 提交于 2021-02-05 07:40:19
问题 I'm trying to make a simple life simulator, and I need the "cells" to move almost randomly (with a few rules) across the screen. But the problem is, after a while they tend to bunch up in the upper-left corner of the screen. I tried to change a lot of things, like completely skipping the rules and making them move completely randomly, but they still bunch up. Is there some obvious problem in my code? Or should I be doing the movement completely differently? The code with irrelevant parts cut

Duplicating a sprite

大憨熊 提交于 2021-02-05 07:33:13
问题 I'm having trouble with getting new sprites to be added. I'm looking for something along the lines of: def duplicate(sprites): for d in sprites: if d.energy >= d.max_energy * 0.9: d.energy = d.energy / 2 new_d = d.duplicate() so if 1 sprite had its 'energy' above 90% of its 'max_energy', its energy would be cut in half and now there would be a second sprite that was identical to the first. I'm not sure how to pull that off though. 回答1: In general, you need to implement the duplicate method

Trying to change image of moving character in every 0.25 seconds PyGame

余生长醉 提交于 2021-02-05 07:33:12
问题 So i am trying to 'animate' my character in pygame by changing between 2 pictures when he walks. I tried to use the code that was mentioned here: In PyGame, how to move an image every 3 seconds without using the sleep function? but it didn't turn out too well. In fact my character only uses one image when walking. here the part of the code and some variables: self.xchange: change on x axis self.img: image for when the character stands still self.walk1 and self.walk2: the two images i am

Implementing smooth motion in 2d space in Pygame

让人想犯罪 __ 提交于 2021-02-05 07:26:26
问题 I want to implement smooth motion that accelerates up to max speed and then decelerates slowly if no keys are pressed, similar to the motion of the space ship in asteroid. Here is my current code for the movement: import pygame pygame.init() display = pygame.display.set_mode((640, 480)) clock = pygame.time.Clock() GRAY = pygame.Color('gray12') display_width, display_height = display.get_size() x = display_width * 0.45 y = display_height * 0.8 x_change = 0 y_change = 0 accel_x = 0 accel_y = 0

Moving a Sprite in the direction of an angle in Pygame [duplicate]

*爱你&永不变心* 提交于 2021-02-05 07:25:06
问题 This question already has answers here : How to turn the sprite in pygame while moving with the keys (1 answer) Image rotation while moving (2 answers) Closed 23 days ago . Im trying to move a sprite in the direction of an angle in pygame, using the left & right arrow keys to change the direction of the sprite but when I press the up key, the sprite just moves right. I have 2 variables that take speed and a velocity calculation and adds them together (vel_x & vel_y), I then add this to the